package com.blb.upload_service.controller;

import com.blb.upload_service.entity.UploadEntity;
import com.blb.upload_service.util.FtpUtil;
import com.blb.upload_service.util.OSSUtil;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.UUID;

@RestController
@RequestMapping("upload")
public class UploadController {

    @PostMapping
    public UploadEntity upload(MultipartFile file) throws IOException {
        String filename = file.getOriginalFilename();
        String suffix = filename.substring(filename.lastIndexOf("."));
        String fname = UUID.randomUUID().toString().replace("-","") + suffix;
        FtpUtil.upload("192.168.7.188",21,
                "ftpuser","123456",
                "/home/ftpuser/www/images",fname,file.getInputStream());
        return new UploadEntity(filename,"http://192.168.7.188/images/" + fname);
    }
}
